package org.itrip.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import org.itrip.dao.Downloader;
import org.itrip.entity.IpLocal;
import org.itrip.service.WebSiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Created by 涛 on 2016/3/16.
 */
@Controller
@RequestMapping(value = {""})
public class IndexController {

    @Autowired
    private WebSiteService webSiteService;

    @RequestMapping(value = {"/", "", "/index"})
    public String toIndex(HttpServletRequest request, HttpSession session){
        //webSiteService.listAll();
        String str = Downloader.getHtml("http://ip.chinaz.com/getip.aspx");
        if (str != null && str.length() > 0){
            IpLocal il = JSON.parseObject(str.trim(), IpLocal.class);
            session.setAttribute("ipinfo", il);
            request.setAttribute("ipinfo", il);
        }

        return "index";
    }

    @RequestMapping(value = {"/saveIpLocal"}, method = RequestMethod.POST)
    public IpLocal saveIpLocal(String iplocal){
        IpLocal il = JSON.parseObject(iplocal.split("var")[0].trim(), IpLocal.class);
        System.out.println(JSON.toJSONString(il));
        return il;
    }
}
